/*
 * Copyright 2013 JROSE
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jrose.core.jdbc.datasource;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;

import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Set;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
abstract public class CurrentRoutingPolicy {
    private static final ThreadLocal<Set<Object>> transactionPolicy = new ThreadLocal<>();
    private static final ThreadLocal<ArrayDeque<RoutingPolicy>> callbackPolicy = new ThreadLocal<>();

    public static RoutingPolicy current() {
        Set<Object> set = transactionPolicy.get();
        if (set != null && !set.isEmpty()) {
            // currently in transaction, return usingMaster directly
            return RoutingPolicy.usingMaster;
        }

        // no transaction, check if is in RoutingPolicyCallback
        ArrayDeque<RoutingPolicy> deque = callbackPolicy.get();
        if (deque != null && !deque.isEmpty()) {
            return deque.peekLast();
        }

        // still no callback found, return usingSlave directly
        return RoutingPolicy.usingSlave;
    }

    public static void openTransaction(Object tx) {
        Assert.notNull(tx);
        synchronized (transactionPolicy) {
            Set<Object> set = transactionPolicy.get();
            if (set == null) {
                set = new HashSet<>();
                transactionPolicy.set(set);
            }
            if (set.contains(tx)) {
                return;
            }
            set.add(tx);
        }
    }

    public static void closeTransaction(Object tx) {
        Assert.notNull(tx);
        synchronized (transactionPolicy) {
            Set<Object> set = transactionPolicy.get();
            if (set != null) {
                set.remove(tx);
                if (set.isEmpty()) {
                    transactionPolicy.remove();
                }
            }
        }
    }

    public static void setRoutingPolicy(RoutingPolicy routingPolicy) {
        Assert.notNull(routingPolicy);
        synchronized (callbackPolicy) {
            ArrayDeque<RoutingPolicy> deque = callbackPolicy.get();
            if (deque == null) {
                deque = new ArrayDeque<>();
                callbackPolicy.set(deque);
            }
            deque.addLast(routingPolicy);
        }
    }

    public static void unsetRoutingPolicy() {
        synchronized (callbackPolicy) {
            ArrayDeque<RoutingPolicy> deque = callbackPolicy.get();
            if (deque != null) {
                deque.removeLast();
                if (deque.isEmpty()) {
                    callbackPolicy.remove();
                }
            }
        }
    }
}
